import $global from '@/module/tipsGroup'
import { urlObj } from '@/api/common/request-name.js'
const Qequest = (url, params, otherParams = {}) => {
  const { interType = 'nomal', method = 'POST' } = otherParams
  const urls = urlObj[interType] + url
  return new Promise((resolve, reject) => {
    const body = params
    const newOptions = {
      header: {},
      method,
      url: urls,
      data: body,
      success: (respon) => {
        if (respon.errMsg === 'request:ok') {
          const { data } = respon
          if (data.code === 200) {
            resolve(data?.data || data?.result || respon)
          } else if (data.status === 200) {
            resolve(data)
          } else if (data.code === 0) {
            resolve(data.data)
          } else {
            $global.$toast('fail', data.message || '操作失败')
            reject('失败')
          }
        }
      },
      fail: (error) => {
        reject('失败', error)
        $global.$toast('fail', '网络错误')
      }
    }
    // 设置请求头
    if (interType === 'nomal') {
      newOptions.header['content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
    } else if (method === 'DELETE') {
      newOptions.header['content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
    }
    uni.request(newOptions)
  })
}
export default Qequest